
package com.kugou.game.sdk.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.interfaces.OnRequestValidateCodeListener;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

/**
 * 描述:安全邮箱找回密码
 *
 * @author wcj
 * @since 2015年8月14日 下午2:32:37
 */
public class GetBackPasswdByEmailFragment extends BaseSDKWorkerFragment {

    private static final int MSG_REFRESH_VIEW = 10000;

    private static final int MSG_START_TIMER = 10001;

    private static final int MSG_REQUEST_GET_PASSWD_EMAIL = 10002;

    private static final int MSG_REQUEST_GET_PASSWD_EMAIL_SUCCESS = 10003;

    private static final int MSG_REQUEST_GET_PASSWD_EMAIL_FAILE = 10004;

    private static final int MSG_SHOW_LOADING_VIEW = 10008;

    private static final int MSG_HIDE_LOADING_VIEW = 10009;

    private String mKugouUserName, mEmail, mEmailWithStar;

    private Button mRequestValidateCodeBtn;

    private TextView mRequestedValidateCodeTips, mUserNameTextView, mEmailTextView;

    private int mTimer;

    ScrollView mScrollView;

    LoadingView mLoadingView;

    public static GetBackPasswdByEmailFragment createFragment() {
        GetBackPasswdByEmailFragment fragment = new GetBackPasswdByEmailFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int layoutId = Rx.layout.kg_fragment_get_back_password_by_email;
        if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
            layoutId = Rx.layout.kg_fragment_get_back_password_by_email_land;
        }
        View view = inflater.inflate(layoutId, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mUserNameTextView = (TextView) view.findViewById(Rx.id.kg_tv_account);

        mEmailTextView = (TextView) view.findViewById(Rx.id.kg_tv_email);

        mRequestValidateCodeBtn = (Button) view.findViewById(Rx.id.kg_btn_request_validate_code);
        mRequestValidateCodeBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendEmptyBackgroundMessage(MSG_REQUEST_GET_PASSWD_EMAIL);
            }
        });

        mRequestedValidateCodeTips = (TextView) view.findViewById(Rx.id.kg_tv_validate_code_tips);

        mScrollView = (ScrollView) view.findViewById(Rx.id.kg_layout_scrollview);

        mLoadingView = (LoadingView) view.findViewById(Rx.id.kg_loading);
    }

    private void showConfimDialog() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(getActivity());
        dialog.setMessage("密码修改成功");
        dialog.setSuccessLogoVisibility(View.VISIBLE);
        dialog.setTextGravity(Gravity.CENTER);
        dialog.setCancelBtnVisibility(false);
        dialog.setOKBtnText("知道了");
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getActivity().finish();
            }
        });
        dialog.show();
    }

    private void requestGetPasswdEmailSuccess() {
        showToast(Html.fromHtml(
                "<font color=#ff333333>已发送验证码到你的安全邮箱：</font>" + "<font color=#249EF4>"
                        + mEmailWithStar + "</font>").toString());
        getActivity().finish();
    }

    public void setData(String userName, String email) {
        mKugouUserName = userName;
        mEmail = email;
        mEmailWithStar = AppUtil.getEmailWithStar(email);
        sendEmptyUiMessage(MSG_REFRESH_VIEW);
    }

    @SuppressLint("NewApi")
    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);

        switch (msg.what) {
            case MSG_REFRESH_VIEW:
                mUserNameTextView.setText("酷狗账号：" + mKugouUserName);
                mEmailTextView.setText(Html.fromHtml("<font color=#ff333333>安全邮箱：</font>"
                        + "<font color=#249EF4>" + mEmailWithStar + "</font>"));
                break;

            case MSG_START_TIMER:
                mRequestValidateCodeBtn.setText(mTimer + "s");
                if (mTimer == 60) {
                    mRequestedValidateCodeTips.setText(Html
                            .fromHtml("<font color=#ff333333>已发送验证码到你的安全邮箱：</font>"
                                    + "<font color=#249EF4>" + mEmailWithStar + "</font>"));
                    sendEmptyUiMessageDelayed(MSG_START_TIMER, 1000);
                } else if (mTimer < 0) {
                    mRequestValidateCodeBtn.setText("重新发送取回密码邮件");
                } else {
                    sendEmptyUiMessageDelayed(MSG_START_TIMER, 1000);
                }

                mTimer--;
                break;

            case MSG_REQUEST_GET_PASSWD_EMAIL_SUCCESS:
                requestGetPasswdEmailSuccess();

                break;

            case MSG_REQUEST_GET_PASSWD_EMAIL_FAILE:
                showToast("发送验证码失败：" + msg.obj);
                break;
            case MSG_SHOW_LOADING_VIEW:
                hideSoftInput(getActivity());
                mScrollView.setVisibility(View.GONE);
                mLoadingView.setVisibility(View.VISIBLE);
                break;
            case MSG_HIDE_LOADING_VIEW:
                mScrollView.setVisibility(View.VISIBLE);
                mLoadingView.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        super.handleBackgroundMessage(msg);

        final Message message = Message.obtain();

        switch (msg.what) {
            case MSG_REQUEST_GET_PASSWD_EMAIL:// 请求验证码
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);
                UserManager.getInstance().requestEmailValidateCode(mKugouUserName, mEmail,
                        new OnRequestValidateCodeListener() {

                            @Override
                            public void onSuccess() {
                                message.what = MSG_REQUEST_GET_PASSWD_EMAIL_SUCCESS;
                            }

                            @Override
                            public void onFailed(String errorMsg, String errorCode) {
                                message.what = MSG_REQUEST_GET_PASSWD_EMAIL_FAILE;
                                message.obj = errorMsg;
                            }
                        });
                sendEmptyUiMessage(MSG_HIDE_LOADING_VIEW);
                sendUiMessage(message);
                break;

            default:
                break;
        }
    }
}
